Please enable JavaScript to view this page.

Education Images
Linux file system commands

Linux file system commands

Linux file system commands

Basic Navigation Commands

  1. pwd — Print working directory (shows the current directory path)

    pwd
  2. ls — List files and directories in the current directory

     
    ls
  3. cd — Change directory

     
    cd /path/to/directory
  4. cd ~ — Go to the home directory

    cd ~
  5. cd .. — Move one directory up

    cd ..
  6. ls -l — List files with detailed information (permissions, owner, size)

    ls -l

File and Directory Management:

  1. mkdir — Create a new directory

    mkdir new_directory
  2. rmdir — Remove an empty directory

    rmdir directory_name
  3. rm — Remove files or directories

    rm filename rm -r directory_name # To remove directories and their contents
  4. cp — Copy files or directories

    cp source_file destination cp -r source_directory destination # For directories
  5. mv — Move or rename files or directories

    mv old_filename new_filename mv source_file /path/to/destination
  6. touch — Create an empty file or update file timestamp

    touch filename
  7. find — Search for files and directories

    find /path/to/search -name "filename"

File Viewing and Editing:

  1. cat — Display file contents

    cat filename
  2. more — View file contents page by page

    more filename
  3. less — View file contents with backward navigation

    less filename
  4. nano — Simple text editor for editing files

    nano filename
  5. vim — Advanced text editor for editing files

    vim filename

File Permissions:

  1. chmod — Change file permissions

    chmod 755 filename # Owner: read, write, execute; others: read, execute
  2. chown — Change file owner and group

    chown user:group filename
  3. chgrp — Change the group ownership of a file

    chgrp group filename

Disk Usage and Information:

  1. df — Show disk space usage of file systems

    df -h # Shows human-readable sizes
  2. du — Display file and directory space usage

    du -sh directory_name # Shows total size of directory
  3. lsblk — List information about block devices

    lsblk
  4. fdisk — Partition a disk

    sudo fdisk /dev/sda # Modify the partitions on disk sda

File System Mounting and Management:

  1. mount — Mount a file system

    sudo mount /dev/sda1 /mnt # Mount the disk partition to /mnt
  2. umount — Unmount a file system

    sudo umount /mnt # Unmount the file system mounted at /mnt
  3. blkid — Display information about available block devices

    sudo blkid

Other Useful Commands:

  1. stat — Display file or file system status

    stat filename
  2. ln — Create symbolic or hard links

    ln -s /path/to/original /path/to/link # Symbolic link ln /path/to/original /path/to/link # Hard link
  3. tar — Create or extract compressed archive files

    tar -cvf archive_name.tar /path/to/directory # Create tar file tar -xvf archive_name.tar # Extract tar file
     

    File Editing and Searching Commands in Linux

    File Editing Commands

    1. nano — Simple terminal-based text editor

      • Edit files directly from the terminal.
      nano filename
    2. vim — Advanced text editor with powerful features

      • A highly customizable text editor for programmers and system administrators.
      vim filename
    3. vi — Default text editor on many Unix-based systems (similar to vim)

      • Edit files in vi using modes (Normal, Insert, Command).
      vi filename
    4. sed — Stream editor for basic text manipulation (find and replace, delete, insert, etc.)

      • Example to replace "apple" with "orange" in a file:
      sed -i 's/apple/orange/g' filename
      • -i flag edits the file in place, and s/apple/orange/g is the search and replace command.
    5. tee — Read from standard input and write to both standard output and files

      • Useful when you want to capture output to a file while also displaying it in the terminal.
      echo "Hello World" | tee file.txt

    File Searching Commands

    1. grep — Search for a pattern within files

      • Searches for matching strings in a file or command output.
      grep "search_pattern" filename
       
      • Example to search for "error" in a log file:
      grep "error" /var/log/syslog
    2. grep -r — Search recursively through directories

      • Search all files in the directory and its subdirectories.
      grep -r "pattern" /path/to/directory
    3. find — Find files and directories based on criteria

      • Search for files by name, type, modification date, etc.
      find /path/to/search -name "filename"
      • Example to find all .txt files:
      find /home/user/ -name "*.txt"
    4. locate — Locate files on the system using a pre-built index

      • Faster search than find as it uses an index, but may be outdated unless updated.
      locate filename
    5. which — Locate the executable of a command

      • Find the path of an executable command.
      which python

    awk Commands for Text Processing

    awk is a powerful tool for processing and analyzing text in files. It operates line by line, splitting each line into fields and performing actions based on patterns.

    1. Basic Syntax

      awk 'pattern { action }' filename
      • pattern: A condition to match (can be a string, regular expression, or condition).
      • action: Commands to execute for each line that matches the pattern.
    2. Print Specific Columns

      • To print specific columns in a file (fields are separated by spaces or tabs by default):
      awk '{print $1, $3}' filename
      • Prints the 1st and 3rd columns from each line of the file.
    3. Print Entire Line

      • To print the entire line:
      awk '{print $0}' filename
    4. Print Line Number with Content

      • Print each line with its line number:
      awk '{print NR, $0}' filename
      • NR is a built-in variable that keeps track of the current line number.
    5. Search for Lines Matching a Pattern

      • Search for lines containing a specific word and print them:
      awk '/pattern/ {print $0}' filename
    6. Sum the Values of a Column

      • Sum the values in a particular column (e.g., 2nd column):
      awk '{sum += $2} END {print sum}' filename
    7. Using awk with Conditionals

      • Print lines where the value in the 3rd column is greater than 100:
      awk '$3 > 100 {print $0}' filename
    8. Field Separator

      • If the columns in a file are separated by something other than spaces (e.g., commas), use the -F option to specify the delimiter:
      awk -F "," '{print $1, $2}' filename # For CSV files
    9. Modify Fields (e.g., change a value)

      • Modify or perform calculations on a specific field (e.g., double the value of the 2nd column):
      awk '{$2 = $2 * 2; print $0}' filename
    10. Print Only Lines with Specific Fields

      • Print lines where the 3rd column is exactly "apple":
      awk '$3 == "apple" {print $0}' filename
    11. Multiple Actions in awk

      • You can use multiple actions in the awk command by separating them with semicolons:
      awk '{print $1; print $2}' filename

    Combining awk with Other Commands

    You can combine awk with other commands using pipes. For example, to search for a pattern and process its output with awk:

    grep "pattern" filename | awk '{print $1, $2}'

    This will search for "pattern" in the file and then process the output to print the first and second columns.